<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('站内信息列表')}" />
    <th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mt-2 mb-2">
    <div class="toolbar" id="toolbar" role="group">
        <form id="formId">
            <div class="form-inline">
                <a class="btn btn-success mr-1" onclick="systemAdd()">
                    <i class="fa fa-plus"></i> 发送信息
                </a>
                <input class="form-control mr-1" placeholder="id/内容" type="text" name="searchKey"/>
                <select class="form-control mr-1" name="status">
                    <option value="">状态</option>
                    <option value="0">正常</option>
                    <option value="1">取消</option>
                </select>
                <button type="button" class="btn btn-primary mr-1" onclick="$.table.search()">
                    <i class="fa fa-search"></i>
                    搜索
                </button>
                <button type="button" class="btn btn-default" onclick="$.form.reset()">
                    <i class="fa fa-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
      <table id="bootstrap-table" class="table-striped"></table>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
    var prefix = "/system/msg";
    $(function() {
        var options = {
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            url: prefix + "/list.json",
            modalName: "信息列表",
            columns: [
                {
                    field: 'id',
                    width: 100,
                    title: 'id'
                },
                {
                    field: 'content',
                    title: '内容'
                },
                {
                    field: 'status',
                    width: 120,
                    align: 'center',
                    title: '状态',
                    formatter: function (value, row, index) {
                        return value == 0 ? '<span class="badge badge-primary">正常</span>' : '<span class="badge badge-danger">取消</span>';
                    }
                },
                {
                    field: 'createTime',
                    width: 200,
                    title: '时间',
                },
                {
                    title: '操作',
                    align: 'center',
                    width: 150,
                    formatter: function (value, row, index) {
                        var actions = [];
                        if(row.status == 0){
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="deleteMsg(\'' + row.id + '\')"><i class="fa fa-remove"></i>取消</a> ');

                        }else {
                            actions.push('<a class="btn btn-danger btn-xs disabled" href="javascript:void(0)"><i class="fa fa-remove"></i> 取消</a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    function systemAdd() {
        layer.prompt({title: '输入信息，并确认', formType: 2}, function (content, index) {
            layer.close(index);
            $.post(prefix + "/add.json", {"content": content}, function (json) {
                if (json.code === 0) {
                    window.location.reload()
                } else {
                    layer.msg(json.msg, {icon: 2});
                }
            })
        });
    }

    function deleteMsg(id) {
        layer.confirm('确定要取消该站内信吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            $.post(prefix + "/del.json", {"id": id}, function (json) {
                if (json.code === 0) {
                    window.location.reload()
                } else {
                    layer.msg(json.msg, {icon: 2});
                }
            })
        });
    }
</script>
</body>
</html>